
import 'package:banyou_app/app/config/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

typedef ValueChangedBlock = void Function(String str, int index);

class NameInput extends StatefulWidget {
  final String hint;
  final ValueChangedBlock? onChanged;
  final ValueChanged<bool>? focusChanged;
  final bool lineStretch;
  final bool obscureText;
  final int? index;

  const NameInput({
    this.hint = "",
    this.onChanged,
    this.focusChanged,
    this.index = 0,
    this.lineStretch = false,
    this.obscureText = false,
  });

  @override
  _NameInputState createState() => _NameInputState();
}

class _NameInputState extends State<NameInput> {
  final _focusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    //是否获取光标的监听
    _focusNode.addListener(() {
      if (widget.focusChanged != null) {
        widget.focusChanged!(_focusNode.hasFocus);
      }
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _input();
  }

  _input() {
    return Container(
      constraints: BoxConstraints(maxHeight: 54.0, minHeight: 54.0),
      decoration: BoxDecoration(
          color: Color(0xFF35363F),
          borderRadius: BorderRadius.all(Radius.circular(6)),
          border: Border.all(color: Color(0xFF35363F))),
      margin: EdgeInsets.symmetric(horizontal: 31),
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(
        horizontal: 15,
      ),
      child: FractionallySizedBox(
        widthFactor: 1,
        child: TextField(
          focusNode: _focusNode,
          maxLines: 1,
          onChanged: (v) {
            if (widget.onChanged != null) {
              widget.onChanged!(v, widget.index ?? 0);
            }
          },
          keyboardAppearance: Brightness.dark,
          obscureText: widget.obscureText,
          keyboardType: TextInputType.name,
          textAlign: TextAlign.center,
          cursorColor: Get.theme.primaryColor,
          style: TextStyle(fontSize: 16.sp, color: ColorConfig.white),
          //输入框样式
          decoration: InputDecoration(
              isCollapsed: true,
              contentPadding: EdgeInsets.zero,
              border: InputBorder.none,
              hintText: widget.hint,
              hintStyle: TextStyle(fontSize: 16.sp, color: Color(0xFFB3B3B3))),
        ),
      ),
    );
  }
}
